﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.IO;

namespace KXmlUIControl
{
    public class XmlTextBoxObject : XmlControlBase
    {
        private string _maxLength = "64";
        public string MaxLength 
        {
            get
            {
                return _maxLength;
            }
            set
            {
                _maxLength = value;
            }
        }

        public string Enter { get; set; }

        public string Click { get; set; }

        public string ToolTip { get; set; }

        public bool NotNull { get; set; }

        public string Voice { get; set; }

        public string KeyBoard { get; set; }

        public string TextAlign { get; set; }

        public bool Multiline { get; set; } = false;

        public XmlTextBoxObject() : base()
        {

        }

        public XmlTextBoxObject(string value)
            : base()
        {

        }

        public override string ToXMLString()
        {
            return "ttt";
        }

        public int GetMaxLength()
        {
            if (string.IsNullOrEmpty(MaxLength))
            {
                return 64;
            }
            int value = 64;
            int.TryParse(MaxLength, out value);
            return value;
        }

        public override void SetValues(Control ctl)
        {
            base.SetValues(ctl);
            SetTextAlign(ctl as TextBox);
            TextBox tb = ctl as TextBox;
            tb.Multiline = Multiline;
        }

        public void SetTextAlign(TextBox tb)
        {
            if (string.IsNullOrEmpty(TextAlign))
            {
                return;
            }
            HorizontalAlignment align = HorizontalAlignment.Center;
            if (Enum.TryParse<HorizontalAlignment>(TextAlign, out align))
            {
                tb.TextAlign = align;
            }
        }

        public override Control ToControl(object parent)
        {
            TextBox tb = new TextBox();
            SetValues(tb);
            if (!string.IsNullOrEmpty(Enter))
            {
                Type t = parent.GetType();
                System.Reflection.MethodInfo methodInfo = t.GetMethod(Enter, BindingFlags.Instance | BindingFlags.NonPublic);
                if (methodInfo != null)
                {
                    Delegate DelegateInstance = StaticTypes.CreateDelegateFromMethodInfoByDelegate<EventHandler>(parent, methodInfo);
                    if (DelegateInstance != null)
                    {
                        EventHandler handler = DelegateInstance as EventHandler;
                        tb.Enter += handler;
                    }        
                }                
            }
            if (!string.IsNullOrEmpty(Click))
            {
                Type t = parent.GetType();
                System.Reflection.MethodInfo methodInfo = t.GetMethod(Click, BindingFlags.Instance | BindingFlags.NonPublic);
                if (methodInfo != null)
                {
                    Delegate DelegateInstance = StaticTypes.CreateDelegateFromMethodInfoByDelegate<EventHandler>(parent, methodInfo);
                    if (DelegateInstance != null)
                    {
                        EventHandler handler = DelegateInstance as EventHandler;
                        tb.Click += handler;
                    }
                }
            }
            return tb;
        }
    }
}

//System.Reflection.MethodInfo methodInfo1 = t.GetMethod("buttonTest_KeyDown111", BindingFlags.Instance | BindingFlags.NonPublic);
//EventHandler<KeyEventArgs> DelegateInstance1 = StaticTypes.CreateDelegateFromMethodInfo<KeyEventArgs>(this, methodInfo1);
//KeyEventHandler handler = new KeyEventHandler(DelegateInstance1);
//buttonTest.KeyDown += handler;
